package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.tasks.LoginTask;
import com.njrsrc.android.tasks.ShowUserInfoTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Base64;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 登录页面
 * 
 * @author Administrator
 * 
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
	private TitleBar mBar;
	private LinearLayout usernameLayout, passwordLayout;
	private EditText usernameEditText, passwordEditText;
	/** 游客、注册按钮 */
	private ImageView visitorImageView, registerImageView;
	private Button loginButton;
	private Button registerBtn,visitBtn;
	private UserInfo loginInfo;
	private ProgressDialog progressDialog;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case MsgID.LOGIN_ERROR:
				// 登录失败
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(LoginActivity.this, loginInfo.getError_message(), Toast.LENGTH_SHORT);
				break;
			case MsgID.LOGIN_FAIL:
				// 登录失败
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(LoginActivity.this, loginInfo.getError_message(), Toast.LENGTH_SHORT);
				break;

			case MsgID.LOGIN_SUCCESS:
				// 登录成功
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(LoginActivity.this, R.string.loginSuc, Toast.LENGTH_SHORT);
				AppApplication.mUserID = loginInfo.getUser_id();
				AppApplication.mToken = loginInfo.getToken();
				AppApplication.mResumeID = loginInfo.getResume_id();
				AppApplication.isLogin = true;
				// 将用户名密码保存到SharedPreferences中,下次自动登录
				Editor editor = preferences.edit();
				editor.putString("user", loginInfo.getLogin_name());
				editor.putString("password", loginInfo.getPassword());
				editor.commit();
				// 获取用户信息
				List<String> params = new ArrayList<String>();
				params.add(AppApplication.mToken);
				params.add(AppApplication.mUserID);
				String url = Helpers.combinaStr(RequestUrlProvider.USER_SHOW, params);
				Object[] args = new Object[3];
				args[0] = mHandler;
				args[1] = url;
				args[2] = loginInfo;
				new ShowUserInfoTask().execute(args);
				break;

			case MsgID.SHOW_USER_INFO_FAIL:
				// 获取用户信息失败
				break;

			case MsgID.SHOW_USER_INFO_SUCCESS:
				// 获取用户信息成功
				AppApplication.mUserName = loginInfo.getLogin_name();
				AppApplication.mHeadUrl = loginInfo.getPic_url();
				AppApplication.hrLetterCount = loginInfo.getHr_letter_count();
				finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
        mBar.setTitleText("登陆");
        mBar.back.setVisibility(View.GONE);
		visitorImageView = (ImageView) findViewById(R.id.login_visitor_img);
		registerImageView = (ImageView) findViewById(R.id.login_register_img);
		usernameLayout = (LinearLayout) findViewById(R.id.layout_username);
		passwordLayout = (LinearLayout) findViewById(R.id.layout_password);
		usernameEditText = (EditText) findViewById(R.id.login_edit_username);
		passwordEditText = (EditText) findViewById(R.id.login_edit_password);
		loginButton = (Button) findViewById(R.id.login_btn_login);
		registerBtn = (Button) findViewById(R.id.register);
		visitBtn = (Button) findViewById(R.id.visit);
		visitorImageView.setOnClickListener(this);
		registerImageView.setOnClickListener(this);
		usernameLayout.setOnClickListener(this);
		passwordLayout.setOnClickListener(this);
		loginButton.setOnClickListener(this);
		registerBtn.setOnClickListener(this);
		visitBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_username:
			// 用户名输入点击区域
			usernameEditText.requestFocus();
			Helpers.showSoftKeyboard(LoginActivity.this, usernameEditText);
			break;
		case R.id.layout_password:
			// 密码输入点击区域
			passwordEditText.requestFocus();
			Helpers.showSoftKeyboard(LoginActivity.this, passwordEditText);
			break;

		case R.id.login_visitor_img:
			// 逛逛按钮
			finish();
			break;
		case R.id.register:
			Intent registerIntent = new Intent();
			registerIntent.setClass(LoginActivity.this, RegisterActivity.class);
			startActivity(registerIntent);
			break;
		case R.id.visit:
			// 逛逛按钮
			finish();
			break;

		case R.id.login_register_img:
			// 动画
			Animation loginBtnAnimation = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.login_btn_translate);
			Animation registerBtnAnimation = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.register_btn_translate);
			registerImageView.startAnimation(loginBtnAnimation);
			loginButton.startAnimation(registerBtnAnimation);
			// 注册按钮
			final Intent regIntent = new Intent();
			regIntent.setClass(LoginActivity.this, RegisterActivity.class);
			TimerTask task = new TimerTask() {
				
				@Override
				public void run() {
					startActivity(regIntent);
					finish();
				}
			};
			new Timer().schedule(task, 150);
			break;
		case R.id.login_btn_login:
			String username = usernameEditText.getText().toString();
			String password = passwordEditText.getText().toString();
			// 登录按钮
			// null true
			// "" true
			// "null" false
			// " " false
			if (TextUtils.isEmpty(username)) {
				Helpers.showToast(LoginActivity.this, R.string.loginUserInputTip, Toast.LENGTH_SHORT);
				return;
			}
			if (TextUtils.isEmpty(password)) {
				Helpers.showToast(LoginActivity.this, R.string.loginPassInputTip, Toast.LENGTH_SHORT);
				return;
			}
			// Base64加密
			password = Base64.encode(password.getBytes());
			// 发送登录请求
			List<String> params = new ArrayList<String>();
			params.add(username);
			params.add(password);
			String url = Helpers.combinaStr(RequestUrlProvider.USER_LOGIN, params);
			loginInfo = new UserInfo();
			loginInfo.setLogin_name(username);
			loginInfo.setPassword(password);
			Object[] args = new Object[3];
			args[0] = mHandler;
			args[1] = url;
			args[2] = loginInfo;
			progressDialog = Helpers.showProgressDialog(LoginActivity.this, R.string.logining);
			new LoginTask().execute(args);
			break;

		default:
			break;
		}
	}
}